/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      StopWords.cpp
 *
 * $FTS$
 */

#include <StopWords.hpp>

// C Includes
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

// Local Includes
#include <LemmatizerInterface.hpp>
#include <STLVector.hpp>
#include <STLString.hpp>

namespace FTSS // FullTextSearchSystem
{

//
// Constructor
//
StopWords::StopWords(LemmatizerInterface & oILemmatizer): oLemmatizer(oILemmatizer), oStopWordHash(1)
{
	;;
}

//
// Add word to stoprword list
//
void StopWords::AddWord(CCHAR_P szWord, const UINT_32 & iWordLength, const INT_32 & iLemmatize)
{
	if (oLemmatizer.CanLemmatize(szWord, iWordLength) == -1)
	{
		TokenHash_t iWordHash = HashFunc(szWord, iWordLength);

		oStopWordHash.insert(StopWordPair(iWordHash, 1), iWordHash);
	}
	else
	{
		using STLW::string;
		using STLW::vector;
		vector<string> vFirstFormsQuick = oLemmatizer.FirstForms(szWord, iWordLength, 1);
		Size_t iWordForms = vFirstFormsQuick.size();
		Size_t iI = 0;
		for(iI = 0; iI < iWordForms; iI++)
		{
			TokenHash_t iWordHash = HashFunc(vFirstFormsQuick[iI].c_str(), vFirstFormsQuick[iI].size());
			oStopWordHash.insert(StopWordPair(iWordHash, 1), iWordHash);
		}
	}
}

//
// Remove word from stoprword list
//
void StopWords::RemoveWord(CCHAR_P szWord, const UINT_32 & iWordLength, const INT_32 & iLemmatize)
{
	if (oLemmatizer.CanLemmatize(szWord, iWordLength) == -1)
	{
		TokenHash_t iWordHash = HashFunc(szWord, iWordLength);
		oStopWordHash.erase(iWordHash);
	}
	else
	{
		using STLW::string;
		using STLW::vector;
		vector<string> vFirstFormsQuick = oLemmatizer.FirstForms(szWord, iWordLength, 1);
		Size_t iWordForms = vFirstFormsQuick.size();
		Size_t iI = 0;
		for(iI = 0; iI < iWordForms; iI++)
		{
			TokenHash_t iWordHash = HashFunc(vFirstFormsQuick[iI].c_str(), vFirstFormsQuick[iI].size());
			oStopWordHash.erase(iWordHash, iWordHash);
		}
	}
}

//
// Check word in stop word list
//
INT_32 StopWords::CheckWord(CCHAR_P szWord, const UINT_32 & iWordLength, const INT_32 & iLemmatize)
{
	if (oLemmatizer.CanLemmatize(szWord, iWordLength) == -1)
	{
		TokenHash_t iWordHash = HashFunc(szWord, iWordLength);

		if (oStopWordHash.find(iWordHash, iWordHash) != oStopWordHash.end()) { return -1; }
	}
	else
	{
		using STLW::string;
		using STLW::vector;
		vector<string> vFirstFormsQuick = oLemmatizer.FirstForms(szWord, iWordLength, 1);
		Size_t iWordForms = vFirstFormsQuick.size();
		Size_t iI = 0;
		for(iI = 0; iI < iWordForms; iI++)
		{
			TokenHash_t iWordHash = HashFunc(vFirstFormsQuick[iI].c_str(), vFirstFormsQuick[iI].size());
			if (oStopWordHash.find(iWordHash, iWordHash) != oStopWordHash.end()) { return -1; }
		}
	}
return 0;
}

//
// Check word in stop word list
//
INT_32 StopWords::CheckWord(const TokenHash_t & iWordHash)
{
	if (oStopWordHash.find(iWordHash, iWordHash) == oStopWordHash.end()) { return 0; }
return -1;
}

//
// Serialize and dump objec
//
INT_32 StopWords::Dump(DataDumper * pObject)
{
	if (pObject -> StartDump(C_STOP_WORDS_TYPE, (CHAR_P)C_STOP_WORDS_READABLE) != 0) { return -1; }

	// Dump data
	StopWordHash::iterator ithData = oStopWordHash.begin();
	while (ithData != oStopWordHash.end())
	{
		// Hash Key
		if (pObject -> Dump(&(ithData -> first), sizeof(TokenHash_t)) != 0) { return -1; }
		ithData++;
	}
	TokenHash_t iDummy = (TokenHash_t)-1;
	if (pObject -> Dump(&iDummy, sizeof(TokenHash_t)) != 0) { return -1; }
	if (pObject -> EndDump() != 0) { return -1; }

return 0;
}

//
// Restore and de-serialize object
//
INT_32 StopWords::Restore(DataDumper * pObject)
{
	if (pObject -> StartRestore(C_STOP_WORDS_TYPE, (CHAR_P)C_STOP_WORDS_READABLE) != 0) { return -1; }
	TokenHash_t iDummy = (TokenHash_t)-1;
	oStopWordHash.clear();
	do
	{
		if (pObject -> Restore(&iDummy, sizeof(TokenHash_t)) != 0) { return -1; }
		oStopWordHash.insert(StopWordPair(iDummy, 1), iDummy);
	} while(iDummy != (TokenHash_t)-1);

	if (pObject -> EndRestore() != 0) { return -1; }

return 0;
}

//
// Destructor
//
StopWords::~StopWords() throw() { ;; }

} // namespace FTSS
// End.
